package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.VideoManager;
import com.itheima.vo.PageBeanVo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class VideoController {

    @Autowired
    VideoManager videoManager;

    //视频列表
    @GetMapping("/smallVideos")
    public ResponseEntity findRecommendVideoByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){

        PageBeanVo pageBeanVo = videoManager.findRecommendVideoByPage(UserHolder.get().getId(),pageNum,pageSize);
        return ResponseEntity.ok(pageBeanVo);
    }

    //保存视频
    @PostMapping("/smallVideos")
    public ResponseEntity save(MultipartFile videoThumbnail,MultipartFile videoFile) throws IOException {
        return videoManager.save(UserHolder.get().getId(),videoThumbnail,videoFile);
    }

    //取消关注
    @PostMapping("/smallVideos/{focusUserId}/userUnFocus")
    public void userUnFocus(@PathVariable Long focusUserId){
        videoManager.userUnFocus(UserHolder.get().getId(),focusUserId);
    }

    //关注
    @PostMapping("/smallVideos/{focusUserId}/userFocus")
    public void userFocus(@PathVariable Long focusUserId){
        videoManager.userFocus(UserHolder.get().getId(),focusUserId);
    }


    /**
     * 视频点赞
     * @param videoId
     */
    @PostMapping("/smallVideos/{videoId}/like")
    public ResponseEntity videoLike(@PathVariable ObjectId videoId){
        return videoManager.videoLike(videoId);
    }

    /**
     * 视频取消点赞
     * @param videoId
     * @return
     */
    @PostMapping("/smallVideos/{videoId}/dislike")
    public ResponseEntity videoDislike(@PathVariable ObjectId videoId){
        return videoManager.videoDislike(videoId);
    }

    /**
     * 视频评论列表
     * @param videoId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/smallVideos/{videoId}/comments")
    public ResponseEntity videoComments(@PathVariable String videoId,
                                        @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
                                        @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){
        return videoManager.videoComments(new ObjectId(videoId),pageNum,pageSize);
    }

    /**
     * 发布视频评论
     * @param videoId
     * @return
     */
    @PostMapping("/smallVideos/{videoId}/comments")
    public ResponseEntity videoComment(@PathVariable String videoId,
                                       @RequestBody Map<String,String> map){
        return videoManager.videoComment(videoId,map);
    }

}
